home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / action.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  3KB  |  103 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9. /* action.c - temporarily pulled out from adaprs.c */
  10.  
  11. #include "hdr.h"
  12. #include "ada.h"
  13. #include "miscp.h"
  14. #include "actionp.h"
  15.  
  16.  
  17. /* Action: Return an action corresponding to a given state and input 
  18.    symbol. Given a state and input symbol, we get a unique value uniquevalue,
  19.    which we use to get a hash value hashvalue. We use hashvalue as an index
  20.    into act_tab1. If the entry is 0, we return the default action for the
  21.    state. If the table entry is < NUM_ACTIONS, then we return either
  22.    the table entry if act_tab2 gives uniquenum at location hashvalue, or
  23.    the default reduction if not. Otherwise we go through act_tab2 starting
  24.    at index uniquevalue until we find uniquevalue or 0, and return what
  25.    is found in act_tab1 at this location or the default reduction,
  26.    respectively. A shift is indicated by the new state to go to after the
  27.    shift, and a reduction is indicated by the rule number + NUM_STATES. */
  28.  
  29.  
  30. int action(int state, int sym)                                    /*;action*/
  31. {
  32.     long uniquenum;
  33.     int hashvalue;
  34.     int i;
  35.     int aval;
  36. #ifdef DEBUGACT
  37.     printf("action state %d sym %d\n", state, sym); /*DEBUG*/
  38. #endif
  39.     if (state < 0) {
  40. #ifdef DEBUG
  41.         printf("action: fatal error state negative %d\n", state);
  42. #endif
  43.         exitp(RC_INTERNAL_ERROR);
  44.     }
  45.     if (sym < 0) {
  46. #ifdef DEBUG
  47.         printf("action: fatal error sym negative %d\n", sym);
  48. #endif
  49.         exitp(RC_INTERNAL_ERROR);
  50.     }
  51. #ifdef DEBUGACT
  52.     uniquenum = state;
  53.     printf("uniq = state %ld\n", uniquenum);
  54.     uniquenum *= NUM_INPUTS;
  55.     printf("uniq = NUM_INT %ld\n", uniquenum);
  56.     uniquenum += sym;
  57.     printf("uniq += sym %ld\n", uniquenum);
  58.     hashvalue = uniquenum % TABLE_SIZE;
  59.     printf("action hash long %ld becomes %d\n", uniquenum, hashvalue);
  60.     aval = act_tab1[hashvalue];
  61.     printf("initial act_tab1 value for %d is %d\n", hashvalue, aval);
  62.     if (act_tab1[hashvalue] == 0) {
  63.         aval = def_action[state - 1];
  64.         printf("action 1st case %d\n", aval);
  65.         return(aval);
  66.     }
  67.     if (act_tab1[hashvalue] < NUM_ACTIONS) {
  68.         if (act_tab2[hashvalue]== uniquenum) {
  69.             aval = act_tab1[hashvalue];
  70.             printf("action 2nd case %d\n", aval);
  71.             return aval;
  72.         }
  73.         else {
  74.             aval = def_action[state-1];
  75.             printf("acton 3rd case %d\n", aval);
  76.             return aval;
  77.         }
  78.     }
  79.     for (i = act_tab1[hashvalue] - NUM_ACTIONS; act_tab2[i] != 0; i++) {
  80.         if (act_tab2[i] == uniquenum) {
  81.             aval = act_tab1[i];
  82.             printf("action 4th case %d %d\n", i, aval);
  83.             return aval;
  84.         }
  85.     }
  86.     aval = def_action[state-1];
  87.     printf("action 5th case %d\n", aval);
  88.     return(aval);
  89. #else
  90.     uniquenum = (long) state * NUM_INPUTS + sym;
  91.     hashvalue = (int)uniquenum % TABLE_SIZE;
  92.     aval = act_tab1[hashvalue];
  93.     if (aval == 0)
  94.         return(def_action[state - 1]);
  95.     if (aval < NUM_ACTIONS)
  96.         return((act_tab2[hashvalue] == uniquenum) ? aval : def_action[state-1]);
  97.     for (i = act_tab1[hashvalue] - NUM_ACTIONS; act_tab2[i] != 0; i++)
  98.         if (act_tab2[i] == uniquenum)
  99.             return(act_tab1[i]);
  100.     return(def_action[state - 1]);
  101. #endif
  102. }
  103.